package com.xxg.renrenproduct.dao;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Indexes;
import com.xxg.renrencommon.domain.bean.CarryProductSettingInfo;
import com.xxg.renrencommon.domain.filedenum.productenum.CarryProductSettingTypeEnum;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * 带货设置信息dao
 *
 * @author szj
 * @date 2021/11/29
 */
@Repository
public class CarryProductSettingInfoDao {
    private final MongoTemplate mongoTemplate;

    public CarryProductSettingInfoDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "carryProductSettingInfo";
        MongoCollection<Document> collection;
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            collection = mongoTemplate.createCollection(CarryProductSettingInfo.class);
            createIndex(collection);
        }
    }

    /**
     * 创建索引
     *
     * @param collection 数据集
     */
    private void createIndex(MongoCollection<Document> collection) {
        collection.createIndex(Indexes.ascending("userId"));
        collection.createIndex(Indexes.ascending("shopId"));
        collection.createIndex(Indexes.ascending("spuId"));
        collection.createIndex(Indexes.compoundIndex(Indexes.ascending("userId"), Indexes.ascending("spuId")));
    }

    public CarryProductSettingInfo findGlobalByUserId(Integer userId) {
        Query query = new Query(where("userId").is(userId).and("type").is(CarryProductSettingTypeEnum.GLOBAL.getValue()));
        return mongoTemplate.findOne(query, CarryProductSettingInfo.class);
    }

    /**
     * 根据userId更新所有带货权限
     *
     * @param userId         userId
     * @param carryAble      是否有带货权限
     * @param carryApplyAble 带货是否需要申请
     */
    public void updateByUserId(Integer userId, Boolean carryAble, Boolean carryApplyAble) {
        Query query = new Query(where("userId").is(userId));
        Update update = new Update();
        update.set("carryAble", carryAble);
        update.set("carryApplyAble", carryApplyAble);
        mongoTemplate.updateMulti(query, update, CarryProductSettingInfo.class);
    }

    public void insert(CarryProductSettingInfo info) {
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        mongoTemplate.insert(info);
    }

    public void save(CarryProductSettingInfo carryProductSettingInfo) {
        mongoTemplate.save(carryProductSettingInfo);
    }

    public CarryProductSettingInfo findByUserIdAndSpuId(Integer userIdInt, Integer spuId) {
        return null;
    }

    /**
     * 统计底下是否有单个设置
     *
     * @param userId
     * @return 数量
     */
    public long countSpuByUserId(String userId) {
        Query query = new Query(where("userId").is(userId).and("spuId").ne(null));
        return mongoTemplate.count(query, CarryProductSettingInfo.class);
    }

    public void updateRatioByUserId(Integer userId, Double minCarryRatio, Double maxCarryRatio) {
        Query query = new Query(where("userId").is(userId));
        Update update = new Update();
        update.set("minCarryRatio", minCarryRatio);
        update.set("maxCarryRatio", maxCarryRatio);
        mongoTemplate.updateMulti(query, update, CarryProductSettingInfo.class);
    }

    public CarryProductSettingInfo findBySpuId(Integer spuId) {
        Query query = new Query(where("spuId").is(spuId));
        return mongoTemplate.findOne(query, CarryProductSettingInfo.class);
    }

    public CarryProductSettingInfo findGlobalByShopId(Integer shopId) {
        Query query = new Query(where("shopId").is(shopId).and("type").is(CarryProductSettingTypeEnum.GLOBAL.getValue()));
        return mongoTemplate.findOne(query, CarryProductSettingInfo.class);
    }
}
